﻿/*queue*/
var MVCNotificationQueue = {
    options:
    {
        position: 'inline'
    },
    initialTopPosition: 0,
    items: [],
    Add: function (newItem) {
        MVCNotificationQueue.items.push(newItem);
        if (newItem.isAttached) 
        {
            $('#' + newItem.targetId).parent().append(newItem.Html());
            newItem.AttachToTarget();
        }
        else {
            $('#msg-wrapper').append(newItem.Html());
        }
        newItem.Close();
    },
    RemoveById: function (notificationId) {
        var itemPosition = 0;
        for (var i = 0; i < MVCNotificationQueue.items.length; i++) {
            if (MVCNotificationQueue.items[i].id == notificationId) {
                itemPosition = i;
                break;
            }
        }
        MVCNotificationQueue.items.splice(itemPosition, 1);
    },
    RemoveByIndex: function (idx) {
        MVCNotificationQueue.items.splice(idx, 1);
    },
    GetObjectById: function (id) {
        for (var i = 0; i < MVCNotificationQueue.items.length; i++) {
            if (MVCNotificationQueue.items[i].id == id ) {
                return MVCNotificationQueue.items[i];
            }
        }
    },
    Init: function (options) {
        var wrapper = $('#msg-wrapper');

        MVCNotificationQueue.initialTopPosition = wrapper.position().top;

        //merge options to existing
        this.options = $.extend({}, this.options, options);

        if (this.options.position != 'inline')
        {
            wrapper.addClass(this.options.position);
        }
        else
        {
            $(window).scroll(function () {

                var windowTop = $(window).scrollTop();

                if (MVCNotificationQueue.initialTopPosition <= windowTop) {
                    wrapper.css({ 'position': 'fixed', 'top': '5px' });
                }
                else {
                    wrapper.css({ 'position': 'static', 'top': 'auto' });
                }

            });
        }


        $('.notification').hover(
          function () {
              try {
                  MVCNotificationQueue.GetObjectById(this.id).Pause();
              } catch (err) { }
          },
          function () {
              try {
                  MVCNotificationQueue.GetObjectById(this.id).Close();
              } catch (err) { }
          });

    }
}

/*notification*/ 
var MVCNotification = function (id, t, d, m, s, w , tad, ia,tgt,grp) {
    this.id = id || new Date().getTime();
    this.type = t || 'info';
    this.duration = d || 0;
    this.initialDuration = d || 0;
    this.message = m || 'message goes here!';
    this.scream = s || 'Hello!';
    this.width = w || 300;
    this.animationBarWith = this.width + 21;
    this.pauseTime = 0;
    this.startTime = 0;
    this.timerAnimationDirection =  tad || 'right';
    this.isAttached = ia;
    this.targetId = tgt || 'none';
    this.grip = grp || 'none';
}


MVCNotification.prototype.Html = function () {
    var self  = this;
    var body = '<div id="' + self.id + '" class="notification" style="width:' + self.animationBarWith + 'px;"> ' +
                  ' <div class="bar-placeholder" style="width:' + self.animationBarWith + 'px;">';
    
    if (self.timerAnimationDirection == 'right') {
        body += ' <a class="bar"></a></div> ';
    }
    else {
        body += ' <a class="bar" style="width:' + self.animationBarWith + 'px;"></a></div> ';
    }               
        body+= ' <div class="alert alert-' + self.type + '" id="msg-container" style="width:' + self.width + 'px;"> ' +
                  ' <a class="close" href="javascript:void(0);" onclick="UserClose(\''+self.id+'\')">×</a> ' +
                  ' <strong>' + self.scream + '</strong> ' +
                     self.message +
                  ' </div> ' +
                 ' </div>';
    return body;
}

MVCNotification.prototype.Close = function () {
    var self = this;

    if (self.duration > 0) {
        self.startTime = new Date();
        $('#' + self.id + ' .bar').animate(
           {
               width: (self.timerAnimationDirection == 'right' ? self.animationBarWith : 0) + 'px'
           },
           self.duration,
           'linear',
           function () {
               $('#' + self.id).fadeOut(350);
               try {
                   MVCNotificationQueue.RemoveById(self.id);
               }
               catch (err) { };
           }
        );
    }
}

MVCNotification.prototype.AttachToTarget = function () {
    var self = this;
    var current = $('#'+self.id);

    var targetCalculatedPosition = GetTargetPosition(self.targetId, self.grip,current.width(),current.height());
    if (targetCalculatedPosition !== undefined) {
        $('#'+self.id).css({
            'position': 'absolute',
            'top': (targetCalculatedPosition.Top) + 'px',
            'left': targetCalculatedPosition.Left + 'px'
        });
    }
}



MVCNotification.prototype.Pause = function () {
    var self = this;
    if (self.duration > 0) {
        $('#' + self.id + ' .bar').stop(true, false);
        self.pauseTime += (new Date() - self.startTime);
        self.duration = (parseInt(self.initialDuration) - parseInt(self.pauseTime));
    }
}




/* notification attachment */
var MVCNotificationPosition  = function(t,l) {
    this.Top = t;
    this.Left = l;
}


function GetTargetPosition(targetId,destination,currentW,currentH) {
    if(targetId !== undefined && destination !== undefined)
    {
        var target = $('#' + targetId);
        if(target.length!==0)
        {
            var top = target.position().top;
            var left = target.position().left;
            var width= target.width();
            var height = target.height();

            switch (destination) {
                case 'top':
                    return new MVCNotificationPosition(top - 10 - currentH, left + (width / 2) - (currentW/2));
                    break;
                case 'bottom':
                    return new MVCNotificationPosition(top + height + 10, left + (width / 2) - (currentW / 2));
                    break;
                case 'right':
                    return new MVCNotificationPosition(top + (height/2) - (currentH/2), left + width +10);
                    break;
                case 'left':
                    return new MVCNotificationPosition(top + (height / 2), left - 10);
                    break;
            }

        }
    }
    return undefined;
}









/*other stuff*/
function UserClose(notificationId) {
    $('#' + notificationId).fadeOut(350);
    try {
        MVCNotificationQueue.RemoveById(notificationId);
    }
    catch (err) { };
}

